package com.lwm.proxy.cglib;

import com.lwm.proxy.agent.ProducerProxy;
import com.lwm.proxy.produce.IProducer;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 基于子类 Enhancer
 * 提供者：第三方 cglib
 * 被代理的类不能是final修饰
 *
 * @author liweimin
 * @date 2021/6/10 17:28
 * @since
 */
public class Client {
    public static void main(String[] args) {
        Producer producer = new Producer();
        /**
         *Class -->加载代理类的字节码
         * Class [] ---》如果有接口，要代理的接口
         * Callback --->增强的功能
         */
        Producer proxyProducer = (Producer) Enhancer.create(Producer.class, new MethodInterceptor() {
            @Override
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {

                System.out.println("方法执行之前");
                Object invoke = method.invoke(producer, args);
                System.out.println("方法执行之后");
                return invoke;
            }
        });
        Class<? extends Producer> proxyProducerClass = proxyProducer.getClass();

        System.out.println(proxyProducerClass.getClassLoader());
        System.out.println(proxyProducerClass);

        proxyProducer.saleProduct(1000f);
        proxyProducer.afterService(12322f);

    }
}
